package controllers;

import java.util.HashMap;
import java.util.List;
import javax.persistence.Query;
import models.Dept;
import models.User;
import models.UserRole;
import play.db.jpa.JPA;
import play.mvc.Controller;

public class Depts extends Controller {

    public static void index() {
        render();
    }

    /**
     * Get expert json.
     */
    public static void json(int page, int pagesize) {

        List<Dept> depts = Dept.all().fetch(page, pagesize);
        long total = Dept.count();

        HashMap<String, Object> obj = new HashMap<String, Object>();
        obj.put("Rows", depts);
        obj.put("Total", total);

        renderJSON(obj);
    }

    /**
     * Get depts json.
     */
    public static void findAll() {
        List<Dept> depts = null;
        depts = Dept.findAll();
        renderJSON(depts);
    }
    /*
     * 根据名称来查找科室
     */

    public static void findByName(String key) {
        List<Dept> lists = Dept.find("name like ?", "%" + key + "%").fetch();
        renderJSON(lists);
    }

    static User connectedUser() {
        String userId = session.get("logged");
        return (userId == null) ? null : (User) User.findById(Long.parseLong(userId));
    }

    /**
     * Update a dept.
     */
    public static void save(Dept dept) {
        dept.save();
        renderJSON("");
    }

    /**
     * Delete a dept.
     */
    public static void delete(long id) {
        Dept.findById(id)._delete();
        renderJSON("");
    }
}
